package com.shop.front.service.user;

import com.shop.front.service.system.SystemService;
import com.shop.front.vo.user.UserSignInVo;
import com.shop.common.enums.System.NewSystemConfigEnum;
import com.shop.common.enums.user.BillTableNameEnum;
import com.shop.common.enums.user.IncomeTypeEnum;
import com.shop.domain.integral.WatchVideosIntegral;
import com.shop.domain.system.NewSystemConfig;
import com.shop.domain.user.User;
import com.shop.domain.user.UserSignIn;
import com.shop.front.dao.integral.WatchVideosIntegralDao;
import com.shop.front.dao.user.UserDao;
import com.shop.front.dao.user.UserSignInDao;
import com.shop.util.DateUtil;
import com.shop.web.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class UserSignInService {

    @Autowired
    private UserSignInDao userSignInDao;
    @Autowired
    private SystemService systemService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private  UserIntegtralBillService userIntegtralBillService;
    @Autowired
    private WatchVideosIntegralDao watchVideosIntegralDao;
    /**
     * 获取用户签到
     * @param userId
     * @return
     */
    public BaseResponse getUserSignIn(Long userId) {


        //获取每日获得积分配置
        List<NewSystemConfig> newSystemConfigList = systemService.getSystemConfigList(NewSystemConfigEnum.USER_SING_IN_ONE);

        //获取昨日时间
        Date yesterDay = DateUtil.dateAddDay(new Date(),-1);
        Date yesStartTime = DateUtil.cutDateYYYYMMDD000000(yesterDay);
        Date yesEndTime = DateUtil.cutDateYYYYMMDD235959(yesterDay);

        //查询用户昨日是否签到
        UserSignIn yesUserSignIn = userSignInDao.findUserSignInByCreateTime(yesStartTime,yesEndTime,userId);

        //查询今日是否签到
        UserSignIn todayUserSignIn = userSignInDao.findUserSignInByCreateTime(DateUtil.cutDateYYYYMMDD000000(new Date()),DateUtil.cutDateYYYYMMDD235959(new Date()),userId);


        List<UserSignInVo> userSignInVos = new ArrayList<UserSignInVo>();

        Boolean isToday = true; //是否今日签到
        Boolean isSignIn = true; //是否历史签到

        for(NewSystemConfig signIn:newSystemConfigList){

            UserSignInVo userSignInVo = new UserSignInVo();

            if(null == yesUserSignIn && null == todayUserSignIn){//昨日未签到 今日未签到
                userSignInVo.setTime(DateUtil.dateAddDay(new Date(),newSystemConfigList.indexOf(signIn)));
                userSignInVo.setSignNum(signIn.getCode());
                userSignInVo.setIntegral(new BigDecimal(signIn.getValue()));
                userSignInVo.setIsToday(isToday);
                userSignInVo.setIsSignIn(false);
                isToday = false;
            }else if(null != yesUserSignIn && null == todayUserSignIn) {//昨日签到  今日未签到
                userSignInVo.setTime(DateUtil.dateAddDay(new Date(),newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn)));
                userSignInVo.setSignNum(signIn.getCode());
                userSignInVo.setIntegral(new BigDecimal(signIn.getValue()));

                if(newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn) < 0){
                    userSignInVo.setIsSignIn(true);
                }else{
                    userSignInVo.setIsSignIn(false);
                }

                if(newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn) == 0){
                    userSignInVo.setIsToday(true);
                }else{
                    userSignInVo.setIsToday(false);
                }
            }else if(null != yesUserSignIn && null != todayUserSignIn){//昨日签到  今日已签到
                userSignInVo.setTime(DateUtil.dateAddDay(new Date(),newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn)));
                userSignInVo.setSignNum(signIn.getCode());
                userSignInVo.setIntegral(new BigDecimal(signIn.getValue()));

                if(newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn) <= 0){
                    userSignInVo.setIsSignIn(true);
                }else{
                    userSignInVo.setIsSignIn(false);
                }

                if(newSystemConfigList.indexOf(signIn) + getBeforeDay(yesUserSignIn) == 0){
                    userSignInVo.setIsToday(true);
                }else{
                    userSignInVo.setIsToday(false);
                }
            }else{//昨日未签到 今日已签到
                userSignInVo.setTime(DateUtil.dateAddDay(new Date(),newSystemConfigList.indexOf(signIn)));
                userSignInVo.setSignNum(signIn.getCode());
                userSignInVo.setIntegral(new BigDecimal(signIn.getValue()));
                userSignInVo.setIsToday(isToday);
                userSignInVo.setIsSignIn(isSignIn);
                isToday = false;
                isSignIn = false;
            }
            userSignInVos.add(userSignInVo);
        }

        return new BaseResponse(userSignInVos);
    }


    /**
     * 用户签到
     * @param userId
     * @return
     */
    public BaseResponse userSignIn(Long userId) {

        UserSignIn todayUserSignIn = userSignInDao.findUserSignInByCreateTime(DateUtil.cutDateYYYYMMDD000000(new Date()),DateUtil.cutDateYYYYMMDD235959(new Date()),userId);

        if(null != todayUserSignIn){
            return BaseResponse.fail("今日已签到，请勿重复签到");
        }

        Date yesterDay = DateUtil.dateAddDay(new Date(),-1);
        Date yesStartTime = DateUtil.cutDateYYYYMMDD000000(yesterDay);
        Date yesEndTime = DateUtil.cutDateYYYYMMDD235959(yesterDay);

        //查询用户昨日是否签到
        UserSignIn yesUserSignIn = userSignInDao.findUserSignInByCreateTime(yesStartTime,yesEndTime,userId);


        //获取今日签到积分配置
        NewSystemConfig toDayConfig = getTodaySignConfig(yesUserSignIn);

        if(null == toDayConfig){
            return BaseResponse.fail("获取签到积分信息异常");
        }

        //用户签到获得积分
        User user = userDao.findSingle(userId);

        if(null == user){
            return BaseResponse.fail("用户信息不存在");
        }

        //需要添加积分
        BigDecimal addIntegral = new BigDecimal(toDayConfig.getValue());

        //保存签到信息
        UserSignIn userSignIn = new UserSignIn();
        userSignIn.setUid(userId);
        userSignIn.setSign_num(toDayConfig.getCode());
        userSignIn.setIntegral(addIntegral);
        userSignIn.setCreateTime(new Date());
        userSignInDao.save(userSignIn);


        userDao.addIntegral(userId,addIntegral);

        //添加积分记录
        BigDecimal refreshIntegral = user.getIntegral().add(addIntegral);
        userIntegtralBillService.addUserIntegralBill(userId, userSignIn.getId(), user.getIntegral(),addIntegral,refreshIntegral,
                BillTableNameEnum.USER_SIGN_IN.getKey(), IncomeTypeEnum.INCOME.getKey(),  "签到获得" + addIntegral +"积分");

        return new BaseResponse();
    }


    private int getBeforeDay(UserSignIn userSignIn){

        if(NewSystemConfigEnum.USER_SING_IN_ONE.getCode().equals(userSignIn.getSign_num())){
            return -1;
        }else if(NewSystemConfigEnum.USER_SING_IN_TWO.getCode().equals(userSignIn.getSign_num())){
            return -2;
        }else if(NewSystemConfigEnum.USER_SING_IN_THREE.getCode().equals(userSignIn.getSign_num())){
            return -3;
        }else if(NewSystemConfigEnum.USER_SING_IN_FOUR.getCode().equals(userSignIn.getSign_num())){
            return -4;
        }else if(NewSystemConfigEnum.USER_SING_IN_FIVR.getCode().equals(userSignIn.getSign_num())){
            return -5;
        }else if(NewSystemConfigEnum.USER_SING_IN_SIX.getCode().equals(userSignIn.getSign_num())){
            return -6;
        }else{
            return -6;
        }

    }

    /**
     * 通过昨日签到信息获取今日签到配置
     * @param yesUserSignIn
     * @return
     */
    private NewSystemConfig getTodaySignConfig(UserSignIn yesUserSignIn){
        //获取积分配置
        List<NewSystemConfig> newSystemConfigList = systemService.getSystemConfigList(NewSystemConfigEnum.USER_SING_IN_ONE);
        NewSystemConfig newSystemConfig = null;
        for(NewSystemConfig config:newSystemConfigList){
            if(null == yesUserSignIn){
                if(config.getCode().equals(NewSystemConfigEnum.USER_SING_IN_ONE.getCode())){
                    newSystemConfig = config;
                }
            }else{

                if(newSystemConfigList.indexOf(config) + getBeforeDay(yesUserSignIn) == 0){
                    newSystemConfig = config;
                }
            }
        }
        return newSystemConfig;
    }


    /**
     * 观看视频 赠送积分
     * @param userId
     * @return
     */
    public BaseResponse watchVideosSendIntegral(Long userId,String vidoesId){

        if(StringUtils.isBlank(vidoesId)){
            return BaseResponse.fail("广告ID不能为空");
        }

        int watchNum = watchVideosIntegralDao.findWatchVideosByCreateTime(DateUtil.cutDateYYYYMMDD000000(new Date()),DateUtil.cutDateYYYYMMDD235959(new Date()),userId);

        if(watchNum >= 10){
            return BaseResponse.fail("今天观看获得积分已超上限");
        }

        //获得看视频获得积分配置
        NewSystemConfig config = systemService.getSystemConfig(NewSystemConfigEnum.WATCH_VIDEOS_INTEGTRAL);

        if(null == config){
            return BaseResponse.fail("获取视频积分信息异常");
        }

        //用户签到获得积分
        User user = userDao.findSingle(userId);

        if(null == user){
            return BaseResponse.fail("用户信息不存在");
        }

        //需要添加积分
        BigDecimal addIntegral = new BigDecimal(config.getValue());

        //保存签到信息
        WatchVideosIntegral watchVideosIntegral = new WatchVideosIntegral();
        watchVideosIntegral.setUid(userId);
        watchVideosIntegral.setMark(config.getRemark());
        watchVideosIntegral.setIntegral(addIntegral);
        watchVideosIntegral.setCreateTime(new Date());
        watchVideosIntegral.setVideosId(vidoesId);
        watchVideosIntegralDao.save(watchVideosIntegral);


        userDao.addIntegral(userId,addIntegral);

        //添加积分记录
        BigDecimal refreshIntegral = user.getIntegral().add(addIntegral);
        userIntegtralBillService.addUserIntegralBill(userId, watchVideosIntegral.getId(), user.getIntegral(),addIntegral,refreshIntegral,
                BillTableNameEnum.WATCH_VIDEOS_INTEGRAL.getKey(), IncomeTypeEnum.INCOME.getKey(),  "观看视频获得" + addIntegral +"积分");

        return new BaseResponse();
    }


}
